package com.mao.dao.warehouse;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.mao.action.jsonvo.warehouse.JsonVoWareHouse;
import com.mao.dao.BaseDAOHibernate;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.impl.warehouse.WareHouseCondition;
import com.mao.util.Validator;

@Component("wareHouseDao")
public class WareHouseDao extends BaseDAOHibernate {
	public List<WareHouse> getAllWareHouse() {
		Session session = this.getSession();
		String hql = "from WareHouse t where t.parent.id = 0 and t.status = " + WareHouse.STATUS_NORMAL;
		Query query = session.createQuery(hql);
		return query.list();
	}

	public WareHouse saveWareHouse(WareHouse w) {
		getHibernateTemplate().save(w);
		return w;
	}

	public WareHouse updateWareHouse(WareHouse w) {
		getHibernateTemplate().update(w);
		return w;
	}
	
	public WareHouse getWareHouseById(long id) {
		WareHouse w = (WareHouse) this.getHibernateTemplate().get(WareHouse.class, id);
		return w;
	}
	
	public List<JsonVoWareHouse> getWarsByWarClass(WareHouseCondition wareHouseCondition, long warClassId,long storeid,Integer page,Integer rows){
		Session session = this.getSession();
		String hql = "From WareHouse w where w.status=:status";
		if(!Validator.isEmpty(wareHouseCondition.getName())){
			hql += " and w.name like :wareName ";
		}
		if(!Validator.isEmpty(wareHouseCondition.getTreePath())){
			hql += " and w.wc.treePath like :treePath ";
		}
//		if(warClassId > 0){
//			hql += " and w.wc.id=" + warClassId;
//		}
		if(storeid > 0){
			hql += " and w.store.id=" + storeid;
		}
		hql += " order by w.id desc";
		Query query = session.createQuery(hql);
		if(!Validator.isEmpty(wareHouseCondition.getTreePath()))
			query.setParameter("treePath", "%"+wareHouseCondition.getTreePath()+"%");
		if(!Validator.isEmpty(wareHouseCondition.getName())){
			query.setParameter("wareName", "%"+wareHouseCondition.getName().trim()+"%");
		}
			
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		query.setParameter("status", WareHouse.STATUS_NORMAL);
		List<WareHouse> list = query.list();
		for (int i = 0; i < list.size(); i++) {
			WareHouse ware = list.get(i);
			if (!Hibernate.isInitialized(ware.getWc())) {
				Hibernate.initialize(ware.getWc());
			}
		}
		List<JsonVoWareHouse> result = new ArrayList<JsonVoWareHouse>();
		JsonVoWareHouse jvwh = null;
		for(WareHouse wh : list) {
			jvwh = new JsonVoWareHouse();
			jvwh.setId(wh.getId());
			jvwh.setName(wh.getName());
			jvwh.setNo(wh.getNo());
			jvwh.setNote(wh.getNote());
			jvwh.setWareClassId(wh.getWc().getId());
			jvwh.setWareClassName(wh.getWc().getName());
			result.add(jvwh);
		}
		return result;
	}
	
	public int getWarsByWarClassCount(WareHouseCondition wareHouseCondition,long warClassId,long storeid){
		Session session = this.getSession();
		String hql = "select count(id) From WareHouse w where w.status=:status";
		if(!Validator.isEmpty(wareHouseCondition.getName())){
			hql += " and w.name like :wareName ";
		}
		if(!Validator.isEmpty(wareHouseCondition.getTreePath())){
			hql += " and w.wc.treePath like :treePath ";
		}
//		if(warClassId > 0){
//			hql += " and w.wc.id=" + warClassId;
//		}
		if(storeid > 0){
			hql += " and w.store.id=" + storeid;
		}
		Query query = session.createQuery(hql);
		if(!Validator.isEmpty(wareHouseCondition.getTreePath()))
			query.setParameter("treePath", "%"+wareHouseCondition.getTreePath()+"%");
		if(!Validator.isEmpty(wareHouseCondition.getName())){
			query.setParameter("wareName", "%"+wareHouseCondition.getName().trim()+"%");
		}
			
		query.setParameter("status", WareHouse.STATUS_NORMAL);
		return ((Long)query.uniqueResult()).intValue(); 
	}
	
	public String valDelWH(long id){
		Session session = this.getSession();
		String hql = "from ProductDeliveryVoucher pdv,ProductWareHouseVoucher whv where (pdv.voucher.wareHouse.id = :id or whv.voucher.wareHouse.id = :id)";
		Query query = session.createQuery(hql);
		query.setParameter("id", id);
		List list = query.list();
		if (list.size() > 0) {
			return "1";
		}
		return "0";
	}
}
